package cn.edu.scau.nursingcenter.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import cn.edu.scau.nursingcenter.bean.ResultBean;
import cn.edu.scau.nursingcenter.entiry.Bed_information;
import cn.edu.scau.nursingcenter.service.impl.Bed_informationServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * 床位信息管理 控制器
 * 
 * @author feng
 *
 */
@Api(tags = "床位信息管理 控制器")
@RestController
@RequestMapping(value = "bed_information")
public class Bed_informationController {

	@Autowired
	private Bed_informationServiceImpl bed_informationServiceImpl;

	/**
	 * 显示所有床位信息
	 * 
	 * @return
	 */
	@ApiOperation(value = "显示所有床位信息")
	@ResponseBody
	@GetMapping(value = "/list")
	public ResultBean list() {
		ResultBean resultBean = null;
		List<Bed_information> bed_informations = bed_informationServiceImpl.selectAll();
		if (bed_informations.size() != 0) {
			resultBean = new ResultBean(200, true, "显示成功", bed_informations);
		} else {
			resultBean = new ResultBean(5009, false, "显示失败", null);
		}
		return resultBean;
	}

	/**
	 * 新增床位
	 * 
	 * @param bed_information
	 * @return
	 */
	@ApiOperation(value = "新增床位")
	@ResponseBody
	@PostMapping(value = "/insert")
	public ResultBean insert(@Validated @RequestBody Bed_information bed_information) {
		ResultBean resultBean = null;
		Bed_information targetBed_information = bed_informationServiceImpl.insert(bed_information);
		if (targetBed_information != null) {
			resultBean = new ResultBean(200, true, "新增成功", targetBed_information);
		} else {
			resultBean = new ResultBean(5008, false, "新增失败", null);
		}
		return resultBean;
	}

	/**
	 * 修改床位信息
	 * 
	 * @param bed_information
	 * @return
	 */
	@ApiOperation(value = "修改床位信息")
	@ResponseBody
	@PostMapping(value = "/update")
	public ResultBean update(@Validated @RequestBody Bed_information bed_information) {
		ResultBean resultBean = null;
		// 判断该客户是否已经有床位
		Bed_information tempBed_information = bed_informationServiceImpl.findByCid(bed_information.getC_id());
		if (tempBed_information != null) {
			resultBean = new ResultBean(5007, false, "床位分配失败，该客户已经有床位了，请勿重复分配", null);
			return resultBean;
		}
		Bed_information targetBed_information = bed_informationServiceImpl.update(bed_information);
		if (targetBed_information != null) {
			resultBean = new ResultBean(200, true, "床位分配成功", targetBed_information);
		} else {
			resultBean = new ResultBean(5007, false, "床位分配失败", null);
		}
		return resultBean;
	}

	/**
	 * 根据名字查询床位信息
	 * 
	 * @param name
	 * @return
	 */
	@ApiOperation(value = "根据名字查询床位信息")
	@ApiImplicitParam(name = "name",value = "客户姓名",required = true, dataType = "String")
	@ResponseBody
	@GetMapping(value = "/find")
	public ResultBean find(@RequestParam(defaultValue = "") String name) {
		ResultBean resultBean = null;
		List<Bed_information> bed_informations = bed_informationServiceImpl.findByName(name);
		if (bed_informations.size() != 0) {
			resultBean = new ResultBean(200, true, "查询成功", bed_informations);
		} else {
			resultBean = new ResultBean(5006, false, "查询失败", null);
		}
		return resultBean;
	}

	/**
	 * 删除床位
	 * 
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "根据编号删除床位")
	@ApiImplicitParam(paramType = "path",name = "id",value = "编号",required = true,dataType = "int")
	@ResponseBody
	@DeleteMapping(value = "/delete/{id}")
	public ResultBean delete(@PathVariable Integer id) {
		ResultBean resultBean = null;
		if (id != null) {
			bed_informationServiceImpl.delete(id);
			resultBean = new ResultBean(200, true, "删除成功", null);
		} else {
			resultBean = new ResultBean(5005, false, "删除失败", null);
		}
		return resultBean;
	}

}
